#!/bin/bash

# 检查是否提供了参数
if [ -z "$1" ]; then
  echo "Usage: $0 <container_suffix>"
  exit 1
fi

# 获取命令行参数并生成容器名称
n=$1
container_name="custom-dind-$n"

# 检查是否存在名为 custom-dind-n 的容器
if [ "$(docker ps -a -q -f name=^/${container_name}$)" ]; then
  echo "Container $container_name already exists. removeing it..."
  docker rm -f "$container_name"
  if [ $? -ne 0 ]; then
    echo "Failed to remove existing container $container_name. Exiting..."
    exit 1
  fi
  echo "Container $container_name removed successfully."
fi

# 运行容器
echo "Starting container with name: $container_name"
docker run -d --name "$container_name" \
  --privileged \
  -p 808"$n":8080 \
  -p 908"$n":9080 \
  -v "$(pwd)/logs":/logs \
  -v "$(pwd)/images":/images \
  custom-dind:latest

# 检查容器是否成功启动
if [ $? -ne 0 ]; then
  echo "Failed to start container. Exiting..."
  exit 1
fi
echo "Container $container_name started successfully."
